﻿using Liang.DataBase.BLL;
using Liang.DataBase.Model.Entity;
using System;
using System.Windows.Forms;

namespace Client
{
    public partial class FrmBasicdataEdit : Form
    {
        public sys_basicdata basicdata { get; set; }

        public FrmBasicdataEdit()
        {
            InitializeComponent();
        }

        public FrmBasicdataEdit(sys_basicdata basicdata)
        {
            InitializeComponent();
            this.basicdata = basicdata;
        }

        private void FrmRefuseTypeEdit_Load(object sender, EventArgs e)
        {
            init();
        }

        public void init()
        {
            if (null == basicdata)
            {
                this.groupBox1.Text = "类型定义-新增";
            }
            else
            {
                this.groupBox1.Text = "类型定义-修改";
                this.txtCode.Text = basicdata.code;
                this.txtName.Text = basicdata.des;
            }
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0xA1 && m.WParam.ToInt32() == 2)
                return;
            base.WndProc(ref m);
        }


        public bool validate()
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("请输入代码");
                this.txtName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtCode.Text))
            {
                MessageBox.Show("请输入编号");
                this.txtCode.Focus();
                return false;
            }

            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                BasicdataBLL basicdataBLL = new BasicdataBLL();

                if (basicdata == null)
                {
                    basicdata = new sys_basicdata()
                    {
                        code = txtCode.Text,
                        des = txtName.Text
                    };

                    basicdataBLL.Insert(basicdata);
                }
                else
                {
                    basicdata.code = txtCode.Text;
                    basicdata.des = txtName.Text;
                    basicdataBLL.Update(basicdata, basicdata.basicdataId);
                }

                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
